package com.mcc.app.group.service.impl;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mcc.app.group.domain.GroupTeam;
import com.mcc.app.group.domain.GroupTeamUser;
import com.mcc.app.group.mapper.GroupTeamMapper;
import com.mcc.app.group.service.IGroupTeamService;
import com.mcc.app.group.service.IGroupTeamUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 班组Service业务层处理
 *
 * @author gxy
 * @date 2021-07-08
 */
@Service
public class GroupTeamServiceImpl implements IGroupTeamService {

    @Autowired
    private GroupTeamMapper groupTeamMapper;

    @Autowired
    private IGroupTeamUserService groupTeamUserService;

    /**
     * 查询班组
     *
     * @param teamId 班组ID
     * @return 班组
     */
    @Override
    public GroupTeam selectGroupTeamById(Long teamId) {
        GroupTeam res = groupTeamMapper.selectById(teamId);
        if (res != null) {
            GroupTeamUser groupTeamUser = new GroupTeamUser();
            groupTeamUser.setTeamId(res.getTeamId());
            res.setUserList(groupTeamUserService.selectGroupTeamUserList(groupTeamUser));
        }
        return res;
    }

    /**
     * 查询班组列表
     *
     * @param groupTeam 班组
     * @return 班组
     */
    @Override
    public List<GroupTeam> selectGroupTeamList(GroupTeam groupTeam) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (ObjectUtil.isNotEmpty(groupTeam)) {
            queryWrapper.like(ObjectUtil.isNotEmpty(groupTeam.getTeamName()), "team_name", groupTeam.getTeamName());
            queryWrapper.eq(ObjectUtil.isNotEmpty(groupTeam.getCode()), "code", groupTeam.getCode());
        }
        queryWrapper.orderByAsc("sort");
        return groupTeamMapper.selectList(queryWrapper);
    }

    /**
     * 新增班组
     *
     * @param groupTeam 班组
     * @return 结果
     */
    @Override
    @Transactional
    public int insertGroupTeam(GroupTeam groupTeam) {
        int res = groupTeamMapper.insert(groupTeam);
        if (res > 0) {
            List<GroupTeamUser> userList = groupTeam.getUserList();
            for (int i = 0; i < userList.size(); i++) {
                GroupTeamUser groupTeamUser = userList.get(i);
                groupTeamUser.setTeamId(groupTeam.getTeamId());
                groupTeamUserService.insertGroupTeamUser(groupTeamUser);
            }
        }
        return res;
    }

    /**
     * 修改班组
     *
     * @param groupTeam 班组
     * @return 结果
     */
    @Override
    @Transactional
    public int updateGroupTeam(GroupTeam groupTeam) {
        int res = groupTeamMapper.updateById(groupTeam);
        if (res > 0) {
            groupTeamUserService.deleteGroupTeamUserByTeamId(groupTeam.getTeamId());
            List<GroupTeamUser> userList = groupTeam.getUserList();
            for (int i = 0; i < userList.size(); i++) {
                GroupTeamUser groupTeamUser = userList.get(i);
                groupTeamUser.setTeamId(groupTeam.getTeamId());
                groupTeamUserService.insertGroupTeamUser(groupTeamUser);
            }
        }
        return res;
    }

    /**
     * 删除班组
     *
     * @param teamId 班组ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteGroupTeamById(Long teamId) {
        int res = groupTeamMapper.deleteById(teamId);
        if (res > 0) {
            groupTeamUserService.deleteGroupTeamUserByTeamId(teamId);
        }
        return res;
    }

    /**
     * 批量删除班组
     *
     * @param teamIds 班组ID
     * @return 结果
     */
    @Override
    public int deleteGroupTeamByIds(Long[] teamIds) {
        if (teamIds.length == 0) return 0;
        return groupTeamMapper.deleteBatchIds(ListUtil.toList(teamIds));
    }
}
